package com.incar.chuxing.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.incar.chuxing.R;
import com.incar.chuxing.adapter.CouponPagerAdapter;
import com.incar.chuxing.api.ApiStore;
import com.incar.chuxing.api.UrlConstant;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.bean.ExchangeCouponBean;
import com.incar.chuxing.bean.MyCouponBean;
import com.incar.chuxing.fragment.MyCouponFragment;
import com.incar.chuxing.utils.Constants;
import com.incar.chuxing.utils.MyApplication;
import com.incar.chuxing.utils.Toast.ToastCompat;
import com.incar.chuxing.utils.zxing.android.CaptureActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CouponsActivity extends BaseActivity {

    @BindView(R.id.editText_myCoupon)
    EditText editTextMyCoupon;
    @BindView(R.id.button_myCoupon)
    Button buttonMyCoupon;
    @BindView(R.id.tablayout_myCoupon)
    TabLayout tablayoutMyCoupon;
    @BindView(R.id.vp_myCoupon)
    ViewPager vpMyCoupon;
    @BindView(R.id.title_more)
    ImageView titleMore;
    private Context mContext = this;
    private List<Fragment> mlist;
    private String[] arrTabTitles;
    private String code;

    @BindView(R.id.title_name)
    TextView titleName;


    @Override
    public int getLayoutId() {
        return R.layout.activity_my_coupon;
    }

    @Override
    public void initView() {
        titleName.setText("优惠券");

        loadNetworkData(10);
    }

    @Override
    public void initData() {
        super.initData();
        mlist = new ArrayList<>();
        mlist.add(MyCouponFragment.newInstance(10));
        mlist.add(MyCouponFragment.newInstance(30));
        mlist.add(MyCouponFragment.newInstance(18));
    }

    private void loadNetworkData(int tabIndex) {
        if (((MyApplication) getApplication()).getUid() == null || ((MyApplication) getApplication()).getUid().equals("")) {
            ToastCompat.makeText(mContext, getResources().getString(R.string.account_expire), Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(mContext, LoginActivity.class);
            startActivity(intent);
            return;
        }
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getAllCoupon(((MyApplication) getApplication()).getRequestFieldMap(), "1", "160", tabIndex + "").enqueue(new Callback<MyCouponBean>() {
            @Override
            public void onResponse(Call<MyCouponBean> call, Response<MyCouponBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200 && response.body().getAttachment() != null) {
                        MyCouponBean.AttachmentBean result = response.body().getAttachment();
                        arrTabTitles = new String[3];
                        arrTabTitles[0] = "可使用(" + result.getCounts() + ")";
                        arrTabTitles[1] = "已使用(" + result.getUsedCounts() + ")";
                        arrTabTitles[2] = "已过期(" + result.getPassCounts() + ")";
                        CouponPagerAdapter adapter = new CouponPagerAdapter(getSupportFragmentManager(), mlist, arrTabTitles);
                        vpMyCoupon.setAdapter(adapter);
                        tablayoutMyCoupon.setupWithViewPager(vpMyCoupon);
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<MyCouponBean> call, Throwable t) {
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @OnClick({R.id.button_myCoupon, R.id.title_more, R.id.title_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.button_myCoupon:
                code = editTextMyCoupon.getText().toString();
                if (!TextUtils.isEmpty(code)) {
                    exchangeCoupon(code);
                } else {
                    ToastCompat.makeText(mContext, getResources().getString(R.string.inputExchange), Toast.LENGTH_SHORT).show();
                }
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Constants.REQUEST_SCAN_COUPON && resultCode == Constants.RESULT_SCAN) {
            //扫码后的字符串
            String code = data.getStringExtra("codedContent");
            Bitmap codedBitmap = data.getParcelableExtra("codedBitmap");
            exchangeCoupon(code);
            codedBitmap.recycle();
        }
    }

    private void exchangeCoupon(String code) {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).exchangeCoupon(((MyApplication) getApplication()).getRequestFieldMap(), code).enqueue(new Callback<ExchangeCouponBean>() {
            @Override
            public void onResponse(Call<ExchangeCouponBean> call, Response<ExchangeCouponBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        ToastCompat.makeText(mContext, getResources().getString(R.string.exchangeSuccess), Toast.LENGTH_SHORT).show();
                        initView();
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<ExchangeCouponBean> call, Throwable t) {
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }


}
